﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour, IReflector
{
    public enum EnemyType
    {
        Normal,
        Woman,
        Fat,
        Bomb,
        Attacker,
        Boss,
        Tree,
    }

    public EnemyType enemyType;
    public bool isMove = false;
    public float moveSpeed = 10f;
    public float moveDis = 4.5f;
    private float time = 0;
    private int leftMove = -1;
    private bool fatMove = false;
    private Vector3 moveDir = Vector3.zero;

    private float boomRange = 5;

    // Use this for initialization
    void Start()
    {
        time = moveDis;
    }

    // Update is called once per frame
    void Update()
    {
        if (enemyType == EnemyType.Fat)
        {
            if (fatMove)
            {
                transform.position += moveDir.normalized * moveSpeed * Time.deltaTime;
                GetComponent<Animation>().Play("roll");
            }
        }
        else
        if (isMove)
        {
            time -= Time.deltaTime;
            if (time <= 0)
            {
                time = moveDis;
                transform.localRotation = Quaternion.Euler(0, 180, 0) * transform.localRotation;
            }
            GetComponent<Animation>().Play("walk");
            transform.Translate(transform.forward * Time.deltaTime * moveSpeed, Space.World);
        }
    }

    public void Exploded()
    {
        Destroy(gameObject);
    }

    private void PlayEffect(EnemyType enemyType)
    {
        switch (enemyType)
        {
            case EnemyType.Normal:
                GameController.instance.InjuredNormal.transform.position = transform.position + new Vector3(0f, 0.6f, 0f);
                GameController.instance.InjuredNormal.SetActive(true);
                GameController.instance.InjuredNormal.GetComponent<ParticleSystem>().Play();
                break;
            case EnemyType.Woman:
                GameController.instance.InjuredYiji.transform.position = transform.position + new Vector3(0f, 0.6f, 0f);
                GameController.instance.InjuredYiji.SetActive(true);
                GameController.instance.InjuredYiji.GetComponent<ParticleSystem>().Play();
                break;
            case EnemyType.Bomb:
                GameController.instance.Boom.transform.position = transform.position + new Vector3(0f, 0.6f, 0f);
                GameController.instance.Boom.SetActive(true);
                GameController.instance.Boom.GetComponent<ParticleSystem>().Play();
                break;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 10 || (enemyType != EnemyType.Fat && other.gameObject.layer == 11 && other.gameObject.GetComponent<Enemy>().enemyType == EnemyType.Fat))
        {
            switch (enemyType)
            {
                case EnemyType.Normal:
                case EnemyType.Attacker:
                    PlayEffect(EnemyType.Normal);
                    GameController.instance.ReduceEnemy(gameObject);
                    PlayJinbi();
                    Destroy(gameObject);
                    break;
                case EnemyType.Woman:
                    PlayEffect(EnemyType.Woman);
                    GameController.instance.player.GetComponent<Player>().flyEff.SetActive(false);
                    GameController.instance.player.GetComponent<Player>().moveSpeed = 0f;
                    GameController.instance.player.GetComponent<Player>().myRenderer.SetActive(false);
                    GameController.instance.ReduceEnemy(gameObject);
                    Destroy(gameObject);
                    UIController.instance.OpenUIFailed();
                    break;
                case EnemyType.Bomb:
                    PlayEffect(EnemyType.Bomb);
                    GameController.instance.player.GetComponent<Player>().moveSpeed = 0;
                    GameController.instance.ReduceTimes();
                    for (int i = 0; i < GameController.instance.walls.Count; i++)
                    {
                        if (GameController.instance.walls[i].GetComponent<WallParent>().wallType == WallType.MUQIANG
                            && Vector3.Distance(GameController.instance.walls[i].transform.position, transform.position) < boomRange)
                        {
                            GameController.instance.walls[i].GetComponent<WallParent>().effect.GetComponent<ParticleSystem>().Play();
                            GameController.instance.walls[i].GetComponent<WallParent>().effect.transform.SetParent(null);
                            GameController.instance.ReduceWall(GameController.instance.walls[i]);
                        }
                    }
                    for (int i = 0; i < GameController.instance.enemies.Count; i++)
                    {
                        if (Vector3.Distance(GameController.instance.enemies[i].transform.position, transform.position) < boomRange)
                        {
                            GameObject goi = GameController.instance.enemies[i];
                            if (GameController.instance.BoomSmall != null)
                            {
                                GameObject goe = Instantiate(GameController.instance.BoomSmall, GameController.instance.enemies[i].transform.position, Quaternion.identity);
                                goe.SetActive(true);
                                goe.GetComponent<ParticleSystem>().Play();
                            }
                            GameController.instance.ReduceEnemy(goi);
                            Destroy(goi);
                        }
                    }
                    GameController.instance.ReduceEnemy(gameObject);
                    Destroy(gameObject);
                    break;
                case EnemyType.Fat:
                    moveDir = transform.position - other.transform.position;
                    transform.localRotation = Quaternion.Euler(moveDir);
                    Vector3 temp = other.GetComponent<Player>().moveDir - moveDir * 100f;
                    other.GetComponent<Player>().moveDir = new Vector3(temp.x, 0, temp.z);
                    other.GetComponent<Player>().moveSpeed = 6;
                    moveSpeed = 8;
                    fatMove = true;
                    break;
                case EnemyType.Boss:
                    StartCoroutine(BossDie());
                    break;
                case EnemyType.Tree:
                    StartCoroutine(TreeTrigger());
                    break;
            }
            if (other.gameObject.layer == 10 && this.enemyType != EnemyType.Tree && this.enemyType != EnemyType.Bomb && this.enemyType != EnemyType.Woman)
                other.gameObject.GetComponent<Player>().StopPlayer();
        }
    }

    public void PlayJinbi()
    {
        GameObject jb = Instantiate(Resources.Load("jinbi"), transform.position + Vector3.up * 0.75f, Quaternion.identity) as GameObject;
        jb.GetComponent<JinBi>().Fly();
    }

    IEnumerator BossDie()
    {
        GameController.instance.player.GetComponent<Player>().moveSpeed = 3;
        GameController.instance.player.GetComponent<Animation>().Play("attack");
        GameObject g = GameObject.FindGameObjectWithTag("MainCamera");
        g.SetActive(false);
        transform.Find("GameObject").gameObject.SetActive(true);
        transform.Find("GameObject").GetComponent<Animation>().Play("DaminCam");
        Time.timeScale = 0.5f;
        yield return new WaitForSeconds(0.6f);
        GameController.instance.BoomDamin.transform.position = transform.position + new Vector3(-0.5f, 0.9f, -1f);
        GameController.instance.BoomDamin.SetActive(true);
        transform.Find("DaMing").gameObject.SetActive(false);
        transform.Find("DaMingDead").gameObject.SetActive(true);
        GameController.instance.ReduceEnemy(gameObject);
        yield return new WaitForSeconds(1f);
        Time.timeScale = 1;
        yield return new WaitForSeconds(0.5f);
        g.SetActive(true);
        GameController.instance.player.GetComponent<Player>().moveSpeed = 10;
        yield return new WaitForSeconds(0.5f);
        UIController.instance.OpenUIReward();
    }

    public Vector3 GetDir()
    {
        return moveDir;
    }

    public void SetDir(Vector3 rV)
    {
        moveDir = rV;
    }

    public void PlayWushiAttack()
    {
        StartCoroutine(WushiAttack());
    }

    IEnumerator TreeTrigger()
    {
        GetComponent<Animation>().Play("Dou");
        if (GetComponentInChildren<ParticleSystem>() != null)
        {
            GetComponentInChildren<ParticleSystem>().gameObject.SetActive(true);
            GetComponentInChildren<ParticleSystem>().Play();
        }
        GameController.instance.player.GetComponent<Player>().flyEff.SetActive(false);
        GameController.instance.player.GetComponent<Player>().moveSpeed = 0f;
        yield return new WaitForSeconds(0.5f);
        GameController.instance.player.GetComponent<Player>().myRenderer.SetActive(false);
        yield return new WaitForSeconds(0.5f);
        GameController.instance.ReduceTimes();
    }

    IEnumerator WushiAttack()
    {
        GetComponent<CapsuleCollider>().enabled = false;
        isMove = false;
        GetComponent<Animation>().Play("attack");
        transform.Find("Eff").gameObject.SetActive(true);
        transform.Find("Eff").GetComponent<ParticleSystem>().Play();
        GameController.instance.player.GetComponent<Player>().myRenderer.SetActive(false);
        GameController.instance.player.GetComponent<Player>().flyEff.SetActive(false);
        GameController.instance.player.GetComponent<Player>().moveSpeed = 0f;
        GameController.instance.player.GetComponent<Player>().PlayEff();
        yield return new WaitForSeconds(0.5f);
        GetComponent<CapsuleCollider>().enabled = true;
        isMove = true;
        GameController.instance.ReduceTimes();
    }
}